let Bullet =require("Bullet");
cc.Class({
    extends: cc.Component,

    properties: {

        BulletFather:{
            default:null,
            type:cc.Node,
        },

        leftGun:{
            default:null,
            type:cc.Node,
        },
        rightGun:{
          default:null,
          type:cc.Node,
        },

        bulletList:{
            default:[],
            type:[cc.Node],
        },

        bullet:{
            default:null,
            type:cc.Prefab,
        },
        bulletNum:10,

        intervalTime:0.5,
        maxNum:9,
        _index:0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {},

    start () {
        //这里面的世界坐标不是基于Canvas的。Canvas应该是单单放UI的，
        //cc.log("本地 "+this.leftGun.x+"  "+this.leftGun.y);
        //cc.log("left zhaunhuan hou  "+this.leftGun.convertToWorldSpace(this.leftGun.position));
        //cc.log("machine "+this.node.convertToWorldSpace(this.leftGun.position));//OK
        //cc.log("bar "+this.node.parent.convertToWorldSpace(this.leftGun.position));

    },

    //当前滑板长度
    Init(length,time){//intervalTime*maxNum 要小于time
        cc.log("子弹初始化");
        this.leftGun.active =true;
        this.rightGun.active =true;

        this.leftGun.x=-(length/2)+(this.leftGun.width*this.leftGun.scaleX)/2;
        this.rightGun.x =(length/2)-(this.rightGun.width*this.rightGun.scaleX)/2;


        if(this.bulletList.length===0){
            this.schedule(this.LoopSpawnBullet,this.intervalTime);
        }else{
            this.SendBulletInCache();
        }
        this.scheduleOnce(function () {
            this.rightGun.active =false;
            this.leftGun.active =false;
        },time);
    },

    LoopSpawnBullet(){
        this._index++;
        //left
        let left= cc.instantiate(this.bullet);
        left.setParent(this.BulletFather);
        left.position =this.node.convertToWorldSpace(this.leftGun.position);
        cc.log("left zhaunhuan hou  "+this.leftGun.convertToWorldSpace(this.leftGun.position));
        this.bulletList.push(left);
        //right
        let ri= cc.instantiate(this.bullet);
        ri.setParent(this.BulletFather);
        ri.position =this.node.convertToWorldSpace(this.rightGun.position); //this.rightGun.convertToWorldSpace();
        this.bulletList.push(ri);

        //道具的使用时间要有一组
        if(this._index>=this.maxNum){
            this.unschedule(this.LoopSpawnBullet,this);
            cc.log("发射完毕");
            this._index =0;
        }
    },

    LoopCacheBullet(){
        this._index++;
        //left
        let left= cc.instantiate(this.bullet);
        left.setParent(this.BulletFather);
        left.position =this.node.convertToWorldSpace(this.leftGun.position);
        this.bulletList.push(left);
        //right
        let ri= cc.instantiate(this.bullet);
        ri.setParent(this.BulletFather);
        ri.position =this.node.convertToWorldSpace(this.rightGun.position);
        this.bulletList.push(ri);

        //道具的使用时间要有一组
        if(this._index>=this.maxNum){
            this.unschedule(this.LoopCacheBullet,this);
            cc.log("发射完毕");
            this._index =0;
        }
    },

    SendBulletInCache(){

        this.schedule(this.LoopCacheBullet,this.intervalTime);
    },

     update (dt) {
        for(let value of this.bulletList)
        {
            let bu= value.getComponent(Bullet);
            bu.UpdateFun(dt);
        }



     },
});
